package fnic.prehand.esagent.passitive_measure;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.configuration.ConfigurationException;

import fnic.prehand.common.AgentConfig;

/**   
 * 内部使用的用于保存对象池中对象的类。   
 * 此类中有两个成员，一个是对象，另一个是指示此对象是否正在使用的标志 。
 */    
class PooledObject {   
//	ObjectPool pools;
	private static final AgentConfig config = AgentConfig.getInstance();
	static int counter =0;
	String objName;
	long optime = 0;

	PassitiveAgent objection = null;// 对象     
    boolean busy = false; // 此对象是否正在使用的标志，默认没有正在使用     

    // 构造函数，根据一个 Object 构告一个 PooledObject 对象     
    public PooledObject(ObjectPool agentPool) {     
//        this.objection = objection;    
//    	pools = agentPool;
        this.objection = new PassitiveAgent(config.getPesIndexName(),config.getPesIndexType(), agentPool);
        counter++;
        objName = "Pool Object["+counter+"]";
        busy = false;
    }     

    // 返回此对象中的对象     
    public PassitiveAgent getObject() {     
        return objection;     
    }     

    // 设置此对象的，对象     
    public void setObject(PassitiveAgent objection) {     
        this.objection = objection;     

    }     

    // 获得对象对象是否忙     
    public boolean isBusy() {     
        return busy;     
    }     

    // 设置对象的对象正在忙     
    public void setBusy(boolean busy) {     
        this.busy = busy; 
        if(busy){
        	optime = System.currentTimeMillis();
        }else{
        	optime = 0;
        }
    }  
    
	public String getObjName() {
		return objName;
	}

	public int ocupy(){
		if(optime>0){
			return (int) (System.currentTimeMillis() - optime);
		}else{
			return -1;
		}
	}
	
public static void main(String[] args) throws Exception {  
	// 加载配置
	try {
		AgentConfig.getInstance().load();
	} catch (ConfigurationException e) {
		System.out.println("load es_agent configuration failed: " + e.getMessage());
		return;
	}
	
    final ObjectPool objPool = new ObjectPool();

    objPool.createPool(); 
    ThreadPoolExecutor scheduler = (ThreadPoolExecutor)Executors.newFixedThreadPool(Math.max(16,Runtime.getRuntime().availableProcessors()));
    ScheduledExecutorService checker = Executors.newSingleThreadScheduledExecutor();
    checker.scheduleAtFixedRate(objPool, 1, 2, TimeUnit.SECONDS);
    while(true){
    	scheduler.execute(new Runnable(){

			public void run() {
				// TODO Auto-generated method stub
				PassitiveAgent agent= objPool.getObject();
				try {
					Thread.sleep((int)(15000*Math.random()));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				objPool.returnObject(agent);
			}
    	});
//    PassitiveAgent obj = objPool.getObject();     
//    objPool.returnObject(obj);
		try {
			Thread.sleep((int)(20));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
//    Thread.sleep(6000);
//    objPool.closeObjectPool();     
//    scheduler.shutdown();
 
}  
}